package mdr.demo

import org.springframework.dao.DataIntegrityViolationException

abstract class InstanceController {

	def InstanceService service = null // MUST be initialized in the subclass
	def instancename = null // MUST be initialized in the subclass
	def propertyName = null
    def className = null
	static defaultAction = 'list'

	def beforeInterceptor = {
	    setup()
        className = "${this.controllerName}" as String
		propertyName = "${className}Instance" as String
	}

	abstract def setup()

    def index = {
        list()
    }

	def list = {
         // if there is an 'id' in the 'params', use it to highlight that item in the list
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def result = service.list(params)
        render view: 'list', model: [(propertyName+"List"): result, (propertyName+"Total"): result.size()]
    }

    def create() {
        def instance = service.newInstance()
        instance.properties = params
        switch (request.method) {
		case 'GET':
            [(propertyName) : instance]
			break
		case 'POST':
	        if (!instance.save(flush: true)) {
	            render view: 'create', model: [(propertyName): instance]
	            return
	        }

			flash.message = message(code: 'default.created.message',
                    args: [message(code: '${propertyName}.label', default: (className)), instance.id])
	        redirect action: 'show', id: instance.id
			break
		}
    }

    def show() {
        def instance = service.getById(params.id)
        if (!instance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: (className)), params.id])
            redirect action: 'list'
            return
        }

        [(propertyName) : instance]
    }
/*
    def edit() {
		switch (request.method) {
		case 'GET':
	        def ${propertyName} = ${className}.get(params.id)
	        if (!${propertyName}) {
	            flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: (className)), params.id])
	            redirect action: 'list'
	            return
	        }

	        [${propertyName}: ${propertyName}]
			break
		case 'POST':
	        def ${propertyName} = ${className}.get(params.id)
	        if (!${propertyName}) {
	            flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: (className)), params.id])
	            redirect action: 'list'
	            return
	        }

	        if (params.version) {
	            def version = params.version.toLong()
	            if (${propertyName}.version > version) {<% def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className) %>
	                ${propertyName}.errors.rejectValue('version', 'default.optimistic.locking.failure',
	                          [message(code: '${propertyName}.label', default: (className))] as Object[],
	                          "Another user has updated this ${className} while you were editing")
	                render view: 'edit', model: [${propertyName}: ${propertyName}]
	                return
	            }
	        }

	        ${propertyName}.properties = params

	        if (!${propertyName}.save(flush: true)) {
	            render view: 'edit', model: [${propertyName}: ${propertyName}]
	            return
	        }

			flash.message = message(code: 'default.updated.message', args: [message(code: '${propertyName}.label', default: (className)), ${propertyName}.id])
	        redirect action: 'show', id: ${propertyName}.id
			break
		}
    }
*/
    def delete() {
        def instance = service.getById(params.id)
        if (!${propertyName}) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: (className)), params.id])
            redirect action: 'list'
            return
        }

        try {
            ${propertyName}.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: '${propertyName}.label', default: (className)), params.id])
            redirect action: 'list'
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: '${propertyName}.label', default: (className)), params.id])
            redirect action: 'show', id: params.id
        }
    }

}